CREATE TABLE [dbo].[tb_TransferenciasDeReestructura]
(
[idTransferencia] [int] NOT NULL IDENTITY(1, 1),
[Reestructura] [int] NOT NULL,
[Agente] [int] NOT NULL,
[Tipo] [int] NOT NULL,
[Licencia] [int] NULL,
[HorasAntes] [int] NULL,
[HorasDespues] [int] NULL,
[Observaciones] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Estado] [int] NOT NULL,
[Ejecutar] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_tb_TransferenciasDeReestructura_Ejecutar] DEFAULT ('S')
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updatetb_TransferenciasDeReestructura] ON [dbo].[tb_TransferenciasDeReestructura]
 FOR UPDATE AS 
 IF (update(idTransferencia)) begin 
	 SELECT * FROM inserted 
	 IF @@ROWCOUNT = 1 begin 
		 UPDATE tb_BajasDeTransferencia
		 SET Transferencia = (SELECT idTransferencia FROM inserted)
		 WHERE tb_BajasDeTransferencia.Transferencia = (SELECT idTransferencia FROM deleted)
		 UPDATE tb_AltasTransferencia
		 SET Transferencia = (SELECT idTransferencia FROM inserted)
		 WHERE tb_AltasTransferencia.Transferencia = (SELECT idTransferencia FROM deleted)
	end  ELSE begin 
		 ROLLBACK TRANSACTION 
 end 
 end
GO
ALTER TABLE [dbo].[tb_TransferenciasDeReestructura] ADD CONSTRAINT [PK_tb_TranseferenciasDeReestructura] PRIMARY KEY NONCLUSTERED  ([idTransferencia]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_TransferenciasDeReestructura] WITH NOCHECK ADD CONSTRAINT [FK_tb_TransferenciasDeReestructura_tb_Agentes] FOREIGN KEY ([Agente]) REFERENCES [dbo].[tb_Agentes] ([idAgente])
GO
ALTER TABLE [dbo].[tb_TransferenciasDeReestructura] WITH NOCHECK ADD CONSTRAINT [FK_tb_TranseferenciasDeReestructura_tb_EstadosDeReestructura] FOREIGN KEY ([Estado]) REFERENCES [dbo].[tb_EstadosDeReestructura] ([idEstadoReestructura])
GO
ALTER TABLE [dbo].[tb_TransferenciasDeReestructura] WITH NOCHECK ADD CONSTRAINT [FK_tb_TranseferenciasDeReestructura_tb_Reestructuras] FOREIGN KEY ([Reestructura]) REFERENCES [dbo].[tb_Reestructuras] ([IDReestructura])
GO
ALTER TABLE [dbo].[tb_TransferenciasDeReestructura] WITH NOCHECK ADD CONSTRAINT [FK_tb_TranseferenciasDeReestructura_tb_TiposDeTransferencia] FOREIGN KEY ([Tipo]) REFERENCES [dbo].[tb_TiposDeTransferencia] ([idTipoTransferencia])
GO
ALTER TABLE [dbo].[tb_TransferenciasDeReestructura] NOCHECK CONSTRAINT [FK_tb_TranseferenciasDeReestructura_tb_Reestructuras]
GO
